home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 253 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  3.3 KB

  1. From: Eugene Lazutkin <eugene@int.com>
  2. Message-ID: <01BAF15D.08B8EEE0@dino.int.com>
  3. X-Original-Date: Fri, 2 Feb 1996 10:55:57 -0600
  4. Path: in1.uu.net!bounce-back
  5. Date: 03 Feb 96 06:36:31 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Organization: -
  8. Newsgroups: comp.std.c++
  9. Subject: RE: Cleaning auto_ptr copy semantics.
  10. Encoding: 91 TEXT
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMRMChOEDnX0m9pzZAQHMqQF+PH9+UUUF58laJsOi7McxjVAMqsJbiLcF
  13.     116iHkKpAdpq8EVD5Z7sEEvPI10Wq/e7
  14.     =eJjv
  15.  
  16. On     Friday, January 26, 1996 9:47 AM,     gregor@netcom.com (Greg Colvin) wrote:
  17.  
  18. > A simple implementation makes the semantics clear:
  19. >    template<class X> class auto_ptr {
  20. >       X* px;
  21. >       mutable bool owner;
  22. >    public:
  23. >       explicit auto_ptr(X* p=0) : px(p), owner(true) {}
  24. >       template<class Y>
  25. >          auto_ptr(const auto_ptr<Y>& r) : px(r.release()), owner(true) {}
  26. >       template<class Y>
  27. >          auto_ptr& operator=(const auto_ptr<Y>& r) {
  28. >             if (&r != this) {
  29. >                if (owner) delete px; else owner = true;
  30. >                px = r.release();
  31. >             }
  32. >             return *this;
  33. >          }
  34. >       ~auto_ptr() { if (owner) delete px; }
  35. >       X& operator*()  const { return *px; }
  36. >       X* operator->() const { return px; }
  37. >       X* get()        const { return px; }
  38. >       X* release()    const { owner = false; return px; }
  39. >    };
  40.  
  41. Sometimes I  implement copy-op?
  42.  
  43. template<class Y>
  44.     auto_ptr& operator=( Y* p ) {
  45.         if( p != px ) {
  46.             if( owner )
  47.                 delete px;
  48.             else
  49.                 owner = true;
  50.             px = p;
  51.         }
  52.     }
  53.  
  54. Of course this is just model suggestion. I'm not sure about constness of 
  55. a pointer. Maybe const pointers suggest that auto_ptr doesn't take ownership.
  56. Anyway this is just a question.
  57.  
  58. Sometimes I need reallocate my object and it looks like:
  59.  
  60.     auto_ptr<MYOBJECT>    x( new MYOBJECT(1) );
  61.     /* ... */
  62.     x = auto_ptr<MYOBJECT>( new MYOBJECT(1) );
  63.  
  64. If x keeps a head of dynamic structure, I need to change the value of the x 
  65. when this dynamic structure is changing (growing or shrinking).  To my the 
  66. current auto_ptr (and your proposal) doesn't provide an elegant solution.  
  67. IMHO, the stright simple assignment operator will work just fine.
  68.  
  69. I didn't find standard equal-operators:
  70.  
  71. template<class T>
  72. bool        operator== ( const auto_ptr<T>& l, const T* r )    
  73.             { return l.get()==r; }
  74.  
  75. template<class T>
  76. bool        operator== ( const T* l, const auto_ptr<T>& r )    
  77.             { return l==r.get(); }
  78.  
  79. template<class T>
  80. bool        operator== ( const auto_ptr<T>& l, const auto_ptr<T>& r )    
  81.             { return l.get()==r.get(); }
  82.  
  83. template<class T>
  84. bool        operator!= ( const auto_ptr<T>& l, const T* r )    
  85.             { return l.get()!=r; }
  86.  
  87. template<class T>
  88. bool        operator!= ( const T* l, const auto_ptr<T>& r )    
  89.             { return l!=r.get(); }
  90.  
  91. template<class T>
  92. bool        operator!= ( const auto_ptr<T>& l, const auto_ptr<T>& r )    
  93.             { return l.get()!=r.get(); }
  94.  
  95. It's pretty convinient.  It's good if you are going to create a STL 
  96. container of auto_ptr's.  In this case you can use STL's algorithms.
  97. Otherwise each programmer should implement this functions manually.
  98.  
  99. Best regards
  100.  
  101.  
  102.  
  103. Eugene Lazutkin
  104. eugene@int.com
  105. ---
  106. [ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  107.   Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  108.   is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
  109.